package atom.core0.util.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import atom.core0.exception.BizSysException;
import atom.core0.object.ObjectUtil;
import sun.misc.BASE64Encoder;

public class JDigest
{


	/**
	 * 利用SHA-1算法生成报文押
	 * 
	 * @param data
	 *            报文数据
	 * @param key
	 *            密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] digestSHA1(byte[] data, byte[] key) throws NoSuchAlgorithmException
	{
		// SHA-1 Hash value of data
		MessageDigest md = MessageDigest.getInstance("SHA-1");


		md.update(data);
		md.update(key);

		return md.digest();
	}

	public static byte[] digestMD5(byte[] data) throws NoSuchAlgorithmException
	{
		// SHA-1 Hash value of data
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(data);
		return md.digest();
	}

	public static byte[] digestMD5(byte[] data, String key) throws BizSysException
	{
		// SHA-1 Hash value of data
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(data);
			if (key != null)
				md.update(key.getBytes());

			return md.digest();
		}
		catch (NoSuchAlgorithmException e)
		{
			throw new BizSysException("MD5不支持的算法异常" + e.getMessage());
		}
	}

	public static byte[] digestMD5(Object data) throws BizSysException
	{
		// SHA-1 Hash value of data
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] aa = ObjectUtil.object2Bytes(data);
			md.update(aa);
			return md.digest();
		}
		catch (NoSuchAlgorithmException e)
		{
			throw new BizSysException("MD5不支持的算法异常" + e.getMessage());
		}
	}

	public static byte[] digestMD5(Object data, String key) throws BizSysException
	{
		try
		{
			// SHA-1 Hash value of data
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] aa;
			if (data instanceof String)
				aa = ((String) data).getBytes();
			else
				aa = ObjectUtil.object2Bytes(data);
			md.update(aa);
			if (key != null)
				md.update(key.getBytes());
			return md.digest();
		}
		catch (NoSuchAlgorithmException e)
		{
			throw new BizSysException("MD5不支持的算法异常" + e.getMessage());
		}
	}



	/**
	 * 适用于上G大的文件
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static byte[] digestMD5(File file) throws IOException, BizSysException
	{
		MessageDigest md;
		try
		{
			md = MessageDigest.getInstance("MD5");
		}
		catch (NoSuchAlgorithmException e)
		{
			throw new BizSysException("初始化失败，MessageDigest不支持MD5");
		}
		FileInputStream in = new FileInputStream(file);
		FileChannel ch = in.getChannel();
		MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());

		md.update(byteBuffer);
		byteBuffer = null;
		in.close();
		ch.close();
		return md.digest();
	}
}
